<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/yzhUpload/control/css/zyUpload.css" type="text/css">
    <style>
        body {
            background-color: #ffffff;
        }

        .zyupload {
            margin: 0;
            height: auto !important;
        }

       .cancel {
           width: 20px;
           height: 20px;
           line-height: 19px;
           border: 1px solid #bcc4caf2;
           background-color: #bcc4caf2;
           border-radius: 50%;
           position: absolute; /* 父组件赋予属性relative生效 */
           right: 10px; /* 使元素处于右上角-右 */
           top: 10px; /* 使元素处于右上角-上 */
       }

        .cancel::before,
        .cancel::after {
            content: "";
            position: absolute;
            height: 14px;
            width: 2px;
            top: 3px; /* 使X处于圆中心 */
            left: 9px; /* 使X处于圆中心 */
            background-color: #353535;
        }

        .cancel::before {
            transform: rotate(45deg);
        }

        .cancel::after {
            transform: rotate(-45deg);
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="config-form">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label required">主键id</label>
        <div class="layui-input-inline">
            <input type="text" name="configId" lay-verify="required" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">活动名称</label>
        <div class="layui-input-inline">
            <input type="text" name="configName" lay-verify="required" lay-reqtext="活动名称不能为空" placeholder="请填写活动名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">配置键名</label>
        <div class="layui-input-inline">
            <input type="text" name="configKey" lay-verify="required" lay-reqtext="配置键名不能为空" placeholder="请填写配置键名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">开始时间</label>
        <div class="layui-input-inline">
            <input type="date" name="startTime" value="{$data_where.startTime}" placeholder="yyyy-MM-dd" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">结束时间</label>
        <div class="layui-input-inline">
            <input type="date" name="endTime" value="{$data_where.endTime}" placeholder="yyyy-MM-dd"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">转向地址</label>
        <div class="layui-input-block">
            <input type="text" name="configLink" placeholder="请填写转向地址" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址来源</label>
        <div class="layui-input-block">
            <select name="configFrom" lay-verify="required" data-value="configFrom">
                <option value="applet">小程序</option>
                <option value="outside">外部链接</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">活动简介</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请填写活动简介"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">活动图</label>
        <div class="layui-input-block">
            <div id="ad-images" style="display: flex;flex-wrap: wrap"></div>
            <input type="text" name="imageLink" value="" hidden>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <div id="zyupload" class="zyupload"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js"></script>
<!-- 引用核心层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/core/zyFile.js"></script>
<!-- 引用控制层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/control/js/zyUpload.js"></script>
<script>

    window.onload=function(){
        let configId = GetQueryString("configId");
        if(isNotNull(configId)){
            init(configId);
            // 初始化插件
            $("#zyupload").zyUpload({
                width: "650px",                 // 宽度
                height: "400px",                 // 宽度
                itemWidth: "140px",                 // 文件项的宽度
                itemHeight: "115px",                 // 文件项的高度
                url: "/meily/upload",  // 上传文件的路径
                fileType: ["jpg", "png", "txt", "js", "exe"],// 上传文件的类型
                fileSize: 51200000,                // 上传文件的大小
                multiple: true,                    // 是否可以多个文件上传
                dragDrop: true,                    // 是否可以拖动上传文件
                tailor: true,                    // 是否可以裁剪图片
                del: true,                    // 是否可以删除文件
                finishDel: false,  				  // 是否在上传文件完成后删除预览
                /* 外部获得的回调接口 */
                onSelect: function(files, allFiles){                    // 选择文件的回调方法
                    console.info("当前选择了以下文件：");
                    console.info(files);
                    console.info("之前没上传的文件：");
                    console.info(allFiles);
                },
                onDelete: function(file, surplusFiles){                     // 删除一个文件的回调方法
                    console.info("当前删除了此文件：");
                    console.info(file);
                    console.info("当前剩余的文件：");
                    console.info(surplusFiles);
                },
                onSuccess: function(file,response){                    // 文件上传成功的回调方法
                    console.info("此文件上传成功：");
                    if(isNotNull(response.url)){
                        uploadAddToImg(response.url);
                    }
                },
                onFailure: function(file){                    // 文件上传失败的回调方法
                    console.info("此文件上传失败：");
                    console.info(file);
                },
                onComplete: function(responseInfo){           // 上传完成的回调方法
                    console.info("文件上传完成");
                    console.info(responseInfo);
                }
            });
        }else{
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    /**
     * 初始化数据
     */
    function init(configId) {
        $.ajax({
            type:"get",
            url:'/activity/config/'+configId,
            dataType:'json',
            contentType: 'application/x-www-form-urlencoded',
            success:function(req){
                layui.use(['form'], function () {
                    let form = layui.form
                    form.val('config-form',{
                        "configId": req.data.configId,
                        "configName": req.data.configName,
                        "configKey": req.data.configKey,
                        "configType": req.data.configType,
                        "configLink": req.data.configLink,
                        "configFrom": req.data.configFrom,
                        "remark": req.data.remark,
                        "startTime": req.data.startTime,
                        "endTime": req.data.endTime,
                        "imageLink": req.data.imageLink
                    });
                    //初始化广告图
                    renderImages(req.data.imageLink);
                })
            },
            error:function(e){
                layer.msg("添加失败");
            }
        })
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert('确定提交修改吗？', {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...",{
                    icon:16,
                    time:-1
                })
                $.ajax({
                    type:"post",
                    url:'/activity/config/save',
                    data:JSON.stringify(data.field),
                    dataType:'json',
                    contentType: 'application/json; charset=UTF-8',
                    success:function(req){
                        layer.msg("修改成功");
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    },
                    error:function(e){
                        layer.msg("修改失败");
                        layer.close(myMsg);//手动关闭
                    }
                })
            });

            return false;
        });

    });

    /**
     * 上传文件完成后将文件地址添加到图片列表中
     * @param url
     */
    function uploadAddToImg(url) {
        let imageLinkVal = $('input[name="imageLink"]').val();
        if(isNotNull(url)) {
            if(isNotNull(imageLinkVal)){
                imageLinkVal += "," + url;
            }else{
                imageLinkVal = url;
            }
            $('input[name="imageLink"]').val(imageLinkVal);
            renderImages(imageLinkVal);
        }
    }

    /**
     * 渲染加载的图片
     */
    function renderImages(imageLinks) {
        $("#ad-images").html("");
        if(!isNotNull(imageLinks)) return;
        let html = '';
        let imgs_arr = imageLinks.split(",");
        if(imgs_arr.length > 0){
            for (const i in imgs_arr) {
                html += '<div style="padding: 20px;position: relative;width: 200px;height: 200px;">' +
                    '                <img width="200px" height="200px" src="'+imgs_arr[i]+'"></img>' +
                    '                <span class="cancel" onclick="delImage('+i+')"></span>' +
                    '            </div>';
            }
        }
        $("#ad-images").html(html);
    }

    /**
     * 删除图片
     */
    function delImage(index) {
        let image_val = $('input[name="imageLink"]').val();
        let new_image_val = '';
        if(isNotNull(image_val)){
            let imgs_arr = image_val.split(",");
            if(imgs_arr.length > 0){
                for (const i in imgs_arr) {
                    if(i == index){
                        continue;
                    }
                    if(isNotNull(new_image_val)){
                        new_image_val += "," + imgs_arr[i];
                    }else{
                        new_image_val = imgs_arr[i];
                    }
                }
            }
        }
        $('input[name="imageLink"]').val(new_image_val);
        renderImages(new_image_val);
    }

    /**
     * 图片上传完成后清空文件夹
     */
    function clear() {
        $.ajax({
            type:"post",
            url:'/meily/file/clear',
            dataType:'json',
            contentType: 'application/json; charset=UTF-8',
            success:function(req){
                console.log(req);
            },
            error:function(e){
                console.log(e);
            }
        })
    }
</script>
</body>
</html>
